﻿using FANUC.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;

namespace FANUC.Controllers
{
    public class TodayAllRobotsOutputPerHourController : ApiController
    {
        private FANUCDB db = new FANUCDB();

        [ResponseType(typeof(TodayAllRobotsOutputsPerHourBarChartDtoOutput))]
        public IHttpActionResult Get()
        {
            #region Test
            if (Helper.IsTest)
            {
                TodayAllRobotsOutputsPerHourBarChartDtoOutput testData = new TodayAllRobotsOutputsPerHourBarChartDtoOutput();
                testData.Date = System.DateTime.Now;
                testData.SlicesInfo = new List<decimal>() { 0, 0, 0, 0, 0, 0, 0, 0, 200M, 300M, 200M, 250M, 300M, 200M, 100M, 150M, 200M, 230M, 280M, 310M, 190M, 0, 0, 0 };

                return Ok(testData);
            }
            #endregion
            DateTime today = System.DateTime.Now.Date;
            var perHourDataArc = (from w in db.Part_Unit.Where(a => a.Start_Time >= today)
                                  group w by new { w.Start_Time.Hour }
                                   into grp
                                  select new
                                  {
                                      grp.Key.Hour,
                                      TheHourTotalOutputProductQuantity = grp.Count(),
                                  }).OrderBy(o => o.Hour).ToList();
            var perHourDataSpot = (from w in db.Spot_Part_Unit.Where(a => a.Start_Time >= today)
                                   group w by new { w.Start_Time.Hour }
                                   into grp
                                   select new
                                   {
                                       grp.Key.Hour,
                                       TheHourTotalOutputProductQuantity = grp.Count(),
                                   }).OrderBy(o => o.Hour).ToList();
            var perHourDataSeal = (from w in db.Seal_Part_Unit.Where(a => a.Start_Time >= today)
                                   group w by new { w.Start_Time.Hour }
                                   into grp
                                   select new
                                   {
                                       grp.Key.Hour,
                                       TheHourTotalOutputProductQuantity = grp.Count(),
                                   }).OrderBy(o => o.Hour).ToList();
            var perHourDataPolish = (from w in db.Polish_Part_Unit.Where(a => a.Start_Time >= today)
                                     group w by new { w.Start_Time.Hour }
                                   into grp
                                     select new
                                     {
                                         grp.Key.Hour,
                                         TheHourTotalOutputProductQuantity = grp.Count(),
                                     }).OrderBy(o => o.Hour).ToList();
            var perHourDataPress = (from w in db.Press_Part_Unit.Where(a => a.Start_Time >= today)
                                    group w by new { w.Start_Time.Hour }
                                   into grp
                                    select new
                                    {
                                        grp.Key.Hour,
                                        TheHourTotalOutputProductQuantity = grp.Count(),
                                    }).OrderBy(o => o.Hour).ToList();

            var perHourData = perHourDataArc.Concat(perHourDataSpot).Concat(perHourDataSeal).Concat(perHourDataPolish).Concat(perHourDataPress);
            perHourData = (from w in perHourData group w by new { w.Hour } into grp select new { grp.Key.Hour, TheHourTotalOutputProductQuantity = grp.Sum(a=>a.TheHourTotalOutputProductQuantity) }).OrderBy(o => o.Hour).ToList();
            if (perHourData.Count() <= 0)
            {
                TodayAllRobotsOutputsPerHourBarChartDtoOutput noData = new TodayAllRobotsOutputsPerHourBarChartDtoOutput();
                noData.Date = System.DateTime.Now;
                noData.SlicesInfo = new List<decimal>() { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                return Ok(noData);
            }

            TodayAllRobotsOutputsPerHourBarChartDtoOutput data = new TodayAllRobotsOutputsPerHourBarChartDtoOutput();
            data.Date = System.DateTime.Now;
            data.SlicesInfo = new List<decimal>() { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            foreach (var item in perHourData)
            {
                data.SlicesInfo[item.Hour] = item.TheHourTotalOutputProductQuantity;
            }

            return Ok(data);
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        public string Options()
        { return null; }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

    }
}
